<?php
namespace app\admin\validate;

use think\Validate;

class Admin extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */	
    protected $rule = [
        'old_password' => 'require',
        'new_password' => 'require|length:6,16',
        'again_password' => 'require|confirm:new_password',
        'username' => 'require|alphaNum|unique:admin|length:3,20',
        'password' => 'require|alphaDash|length:6,34',
        'nickname' => 'require|chsAlphaNum|unique:admin|max:30',
    ];
    
    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */	
    protected $message = [
        'old_password.require' => '旧密码不能为空',
        'new_password.require' => '新密码不能为空',
        'new_password.length' => '新密码长度为6到16个字符',
        'again_password.require' => '请输入确认密码',
        'again_password.confirm' => '两次输入的新密码不一样',
        'username.require' => '账号不能为空',
        'username.alphaNum' => '账号组成只能为为字母和数字',
        'username.unique' => '这个账号已经存在了',
        'username.length' => '账号的长度为3~20',
        'password.require' => '密码不能为空',
        'password.alphaDash' => '密码组成只能为字母和数字，下划线"_"及破折号"-"',
        'password.length' => '密码的长度为6~16',
        'nickname.require' => '昵称不能为空',
        'nickname.chsAlphaNum' => '昵称组成只能是汉字、字母和数字',
        'nickname.unique' => '这个昵称已经存在了',
        'nickname.max' => '昵称长度不能超过30',
    ];
    
    protected $scene = [
        'resetPassword'  =>  ['old_password', 'new_password', 'again_password'],
        'add' => ['username', 'password', 'nickname'],
        //'edit' => ['username', 'password', 'nickname'],
    ];
    public function sceneEdit(){
        return $this->only(['username', 'password', 'nickname'])
                ->remove('password', 'require');
    }
}
